<!--  -->
<template>
  <div class="float-left">
    <div class="float-left j-b-flex mb-30">
      <div class="d-flex">
        <el-button type="primary" @click="showUserAddWin = true">
          添加学员
        </el-button>
        <el-button type="primary" @click="importDialog = true"
          >批量导入
        </el-button>
      </div>
      <div class="d-flex">
        <div>
          <el-input
            class="w-150px"
            v-model="filter.user_id"
            placeholder="学员ID"
          ></el-input>
        </div>
        <div class="ml-10">
          <el-date-picker
            :picker-options="pickerOptions"
            v-model="subscribed_at"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="订阅时间-开始"
            end-placeholder="订阅时间-结束"
          >
          </el-date-picker>
        </div>
        <div class="ml-10">
          <el-button @click="paginationReset()">清空</el-button>
          <el-button @click="firstPageLoad()" type="primary"> 筛选 </el-button>
          <el-button @click="importexcel" type="primary">导出表格</el-button>
        </div>
      </div>
    </div>

    <el-table
      :header-cell-style="{ background: '#f1f2f9' }"
      :data="list"
      class="float-left"
    >
      <el-table-column label="学员ID" prop="user_id" width="120">
      </el-table-column>
      <el-table-column label="学员">
        <template slot-scope="scope">
          <div v-if="users[scope.row.user_id]" class="user-item d-flex">
            <div class="avatar">
              <img
                :src="users[scope.row.user_id].avatar"
                width="40"
                height="40"
              />
            </div>
            <div class="ml-10">
              {{ users[scope.row.user_id].nick_name }}
            </div>
          </div>
          <span v-else class="c-red">学员已删除</span>
        </template>
      </el-table-column>
      <el-table-column label="价格" width="200">
        <template slot-scope="scope">
          <span v-if="scope.row.charge == 0">-</span>
          <span v-else>￥{{ scope.row.charge }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订阅时间" width="200">
        <template slot-scope="scope">{{
          scope.row.created_at | dateFormat
        }}</template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-link type="danger" @click="delItem(scope.row)"> 删除 </el-link>
        </template>
      </el-table-column>
    </el-table>

    <div class="float-left mt-30 text-center">
      <el-pagination
        @size-change="paginationSizeChange"
        @current-change="paginationPageChange"
        :current-page="pagination.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <UserAddComp
      :show="showUserAddWin"
      @close="showUserAddWin = false"
      @confirm="userAddChange"
    ></UserAddComp>
    <UserImportComp
      :show="importDialog"
      :id="id"
      type="vod"
      @close="importDialog = false"
      @change="getData"
    ></UserImportComp>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import moment from "moment";
import UserAddComp from "@/components/user-add";
import UserImportComp from "@/components/user-import";
import XLSX from "xlsx";

export default {
  components: {
    UserAddComp,
    UserImportComp,
  },
  props: ["id"],
  data() {
    //这里存放数据
    return {
      importDialog: false,
      pagination: {
        page: 1,
        size: 10,
      },
      filter: {
        user_id: null,
        subscribe_start_at: null,
        subscribe_end_at: null,
      },
      subscribed_at: null,
      list: [],
      total: 0,
      users: [],
      showUserAddWin: false,

      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    subscribed_at(newVal) {
      if (newVal) {
        this.filter.subscribe_start_at = newVal[0];
        this.filter.subscribe_end_at = newVal[1];
      } else {
        this.filter.subscribe_start_at = null;
        this.filter.subscribe_end_at = null;
      }
    },
  },
  //方法集合
  methods: {
    firstPageLoad() {
      this.pagination.page = 1;
      this.getData();
    },
    paginationReset() {
      this.pagination.page = 1;
      this.filter.user_id = null;
      this.subscribed_at = null;
      this.filter.subscribe_start_at = null;
      this.filter.subscribe_end_at = null;
      this.getData();
    },
    paginationSizeChange(size) {
      this.pagination.size = size;
      this.getData();
    },
    paginationPageChange(page) {
      this.pagination.page = page;
      this.getData();
    },
    getData() {
      let params = {};
      Object.assign(params, this.pagination, this.filter);
      this.$request
        .get(`course/${this.id}/subscribes`, { params: params })
        .then((res) => {
          this.list = res.data.data.data;
          this.total = res.data.data.total;
          this.users = res.data.users;
        });
    },
    delItem(item) {
      this.$confirm("确认操作？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$request
            .get(`course/${this.id}/subscribe/delete`, {
              params: {
                user_id: item.user_id,
              },
            })
            .then(() => {
              this.$message.success(this.$t("common.success"));
              this.getData();
            })
            .catch((e) => {
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    userAddChange(rows) {
      let ids = [];
      rows.forEach((item) => {
        ids.push(item.id);
      });
      this.$request
        .post(`course/${this.id}/subscribe/create`, {
          user_id: ids,
        })
        .then(() => {
          this.$message.success(this.$t("common.success"));
          this.getData();
          this.showUserAddWin = false;
        })
        .catch((e) => {
          this.$message.error(e.message);
        });
    },
    importexcel() {
      if (this.loading) {
        return;
      }
      this.loading = true;

      let params = {
        page: 1,
        size: this.total,
      };
      Object.assign(params, this.filter);

      this.$request
        .get(`course/${this.id}/subscribes`, { params })
        .then((res) => {
          if (res.data.data.total === 0) {
            this.$message.error("数据为空");
            this.loading = false;
            return;
          }
          let users = res.data.users;
          let filename = "录播课程订阅学员.xlsx";
          let sheetName = "sheet1";

          let data = [];
          res.data.data.data.forEach((item) => {
            let user = users[item.user_id];
            data.push({
              学员ID: item.user_id,
              学员: user.nick_name,
              手机号: user.mobile,
              价格: item.charge === 0 ? "-" : "￥" + item.charge,
              时间: item.created_at
                ? moment(item.created_at).format("YYYY-MM-DD HH:mm")
                : "",
            });
          });
          const new_sheet = XLSX.utils.json_to_sheet(data);
          const new_book = XLSX.utils.book_new();
          new_sheet["!cols"] = [
            { wch: 10 },
            { wch: 20 },
            { wch: 15 },
            { wch: 20 },
            { wch: 20 },
          ];
          XLSX.utils.book_append_sheet(new_book, new_sheet);
          XLSX.writeFile(new_book, "录播课程订阅学员.xlsx");
          this.loading = false;
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getData();
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
</style>